package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

/**
 * 所有没有登录的条件下的测试用例，都写在这个类里
 */
public class PageByNoLogin extends Utils {
    public static String listFail = "http://124.223.114.169:8080/blog_list.html";
    public static String editFail = "http://124.223.114.169:8080/blog_edit.html";
    public static String detailFail = "http://124.223.114.169:8080/blog_detail.html?blogId=1";


    /*public PageByNoLogin(String url) {
        super(url);
    }*/
    public PageByNoLogin() {
        super("");//这里情况特殊，先啥都不传,后边用到哪个页面，用driver.get()传对应的url
    }

    public void ListPageByNoLogin() throws IOException, InterruptedException {
        Thread.sleep(2000);
        driver.get(listFail);
        //列表未登录处理
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        //处理警告弹窗
        Alert alert = wait.until(ExpectedConditions.alertIsPresent());
        alert.accept();
        //跳转到登录页面——判断是否成功跳转到登录页面——根据页面内容/标题等
        String expect = driver.getTitle();
            //这里可以截张图看一下
        getScreenShot(getClass().getName());
        assert expect.equals("登录页面");
    }

    public void DetailPageByNoLogin() throws IOException {
        //TODO
        driver.get(detailFail);
        //没有弹窗，会直接跳转到登录页面
        String expect = driver.getCurrentUrl();
        assert expect.equals("http://124.223.114.169:8080/blog_login.html");



    }

    public void EditPageByNoLogin() throws IOException, InterruptedException {
        //TODO
        driver.get(editFail);
        WebElement ele = driver.findElement(By.cssSelector("#submit"));//可以进入编辑页
        driver.findElement(By.cssSelector("#title")).sendKeys("aaaaa");//输入文章标题
        ele.click();//未登录的情况下，点击发布
        //应该跳转到用户登录页面
        String expect = driver.getTitle();
        assert expect.equals("http://124.223.114.169:8080/blog_login.html");
        //回退应该到有刚才输入的标题的页面——内容起码不会一下丢失
        driver.navigate().back();
        driver.get(editFail);
        String expect_articleTitle = driver.findElement(By.cssSelector("#title")).getText();
        assert expect_articleTitle.equals("aaaaa");
    }

}



